package mytest;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @program: thinkinginjava
 * @description: 1
 * @packagename: mytest
 * @author: xzs
 * @date: 2021-03-24 21:37
 **/

public class ScheduledThreadPoolExecutorTest {
    private static final ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            executor.scheduleAtFixedRate(() -> {
                        Date a = new Date();

                        System.out.println(DateUtils.formatTimeStr(a) + Thread.currentThread() + "123");
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    , 10, 300, TimeUnit.MILLISECONDS);
        }
        for (int i = 0; i < 5; i++) {
            executor.scheduleWithFixedDelay(() -> {
                        Date a = new Date();
                        System.out.println(DateUtils.formatTimeStr(a)+Thread.currentThread() + "456");
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    , 30, 300, TimeUnit.MILLISECONDS);
        }
        System.out.println("end");
    }
}
